<!-- 职位档案在职人员 -->
<template>
  <div class="wrap">
    <a-input-search
      :style="{ width: '320px', marginBottom: '20px' }"
      placeholder="请输入姓名"
      search-button
      v-model="searchVal"
      @search="onSearch"
      @press-enter="onSearch"
    >
      <template #button-icon>
        <icon-search />
      </template>
    </a-input-search>
    <TableTurn :tableData="getTableData" ref="tableRef">
      <a-table-column title="姓名" data-index="name"></a-table-column>
      <a-table-column title="人员编号" data-index="jobNumber"></a-table-column>
      <!-- <a-table-column title="职位名称" data-index="headcount"></a-table-column> -->
      <a-table-column
        title="主/兼职类型"
        data-index="workType"
      ></a-table-column>
      <a-table-column title="部门" data-index="department">
        <template #cell="{ record }">
          {{ deptList.find((item: any) => item.id == record.department)?.name }}
        </template>
      </a-table-column>
      <a-table-column title="职务" data-index="zhiWu">
        <template #cell="{ record }">
          {{ jobList.find((item: any) => item.id == record.zhiWu)?.name }}
        </template>
      </a-table-column>
      <a-table-column title="职级" data-index="level">
        <template #cell="{ record }">
          {{ levelList.find((item: any) => item.id == record.level)?.name }}
        </template>
      </a-table-column>
      <a-table-column title="入职时间" data-index="time"> </a-table-column>
    </TableTurn>
  </div>
</template>

<script lang="ts" setup>
import { GetPositionLevelList } from '@/apis/management/organization/baseSetting';
import {
  GetPositionOnJob,
  GetJobList
} from '@/apis/management/organization/positionManagement';
import { getFlatDept } from '@/utils/common';

const route = useRoute();

// 档案id
let id = Number(route.query.id);
let ownerType = Number(route.query.ownerType);
onBeforeRouteUpdate((to) => {
  id = Number(to.query.id);
  ownerType = Number(to.query.ownerType);
  getData();
});

let tableRef = ref();
let searchVal = ref('');
// 获取列表数据
const getTableData = async (pageData?: any) => {
  try {
    const res = await GetPositionOnJob({
      ...pageData,
      id,
      name: searchVal.value
    });
    return Promise.resolve(res);
  } catch (error: unknown) {
    console.log(error);
  }
};

let deptList: any = ref([]); // 储存部门数据
let jobList: any = ref([]); // 储存职务数据
let levelList: any = ref([]);
const getData = async () => {
  try {
    // 获取部门数据
    deptList.value = await getFlatDept();
    // 获取职务数据
    const { content: jobListRes } = await GetJobList({
      pageNumber: 0,
      pageSize: -1
    });
    jobList.value = jobListRes;

    //  获取职位层级数据
    const { content: levelListRes } = await GetPositionLevelList({
      pageNumber: 0,
      pageSize: -1
    });
    levelList.value = levelListRes;
  } catch (error: unknown) {
    console.log(error);
  }
};
getData();

const onSearch = () => {
  tableRef.value.loadData();
};
</script>

<style scoped lang="less"></style>
